package scheme;
import it.unisa.dia.gas.jpbc.PairingParameters;
import util.ConfigFileName;
import accessstruct.Tool;
import accessstruct.Node;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import util.TreeNode;
import util.TreeUtil;
import static accessstruct.Tool.loadPropFromFile;

/**
 * CA
 * 主要功能：
 * 1.系统初始化
 */
public class CA {

    /**
     * CA初始化
     * @param config
     * @return
     */
    public static long setup(ConfigFileName config) {
        Properties AAskProp = new Properties();
        Tool.storePropToFile(AAskProp, config.getAASKFileName());
        Properties AApkProp = new Properties();
        Tool.storePropToFile(AApkProp, config.getAAPKFileName());
        Properties CSPUAkProp = new Properties();
        Tool.storePropToFile(CSPUAkProp, config.getCSPUAKFileName());
        Properties UserTKProp = new Properties();
        Tool.storePropToFile(UserTKProp, config.getTKFileName());
        Properties CSPUAKProp = new Properties();
        Tool.storePropToFile(CSPUAKProp, config.getCSPUAKFileName());
        Properties RSUTKProp = new Properties();
        Tool.storePropToFile(RSUTKProp, config.getTKFileName());
        Properties ctProp = new Properties();
        Tool.storePropToFile(ctProp, config.getCtFileName());
        Properties CApkProp = new Properties();//
        Tool.storePropToFile(RSUTKProp, config.getTKFileName());
        Properties USKPRrop = new Properties();
        Tool.storePropToFile(USKPRrop, config.getUserSKFileName());

        long startTime = System.currentTimeMillis();//
        Pairing bp = PairingFactory.getPairing(config.getPairingParametersFileName());//
        Element g = bp.getG1().newRandomElement().getImmutable();//
        Element w = bp.getG1().newRandomElement().getImmutable();//
        Element v = bp.getG1().newRandomElement().getImmutable();//
        long endTime = System.currentTimeMillis();//
        //生成系统公钥和私钥并存入prop文件之中
        Properties GPProp = loadPropFromFile(config.getGPFileName());
        //公共参数
        GPProp.setProperty("g", Base64.getEncoder().withoutPadding().encodeToString(g.toBytes()));//
        GPProp.setProperty("w", Base64.getEncoder().withoutPadding().encodeToString(w.toBytes()));//
        GPProp.setProperty("v", Base64.getEncoder().withoutPadding().encodeToString(v.toBytes()));//
        //开始存储
        Tool.storePropToFile(GPProp, config.getGPFileName());
        return endTime - startTime;
    }
}

